package com.micmac.customer.model
{
	import com.micmac.customer.vo.CorpVO;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	
	import org.robotlegs.mvcs.Actor;

	public class CorpModel extends Actor
	{
		
		public static const CORP_EDITED:String = 'corp_edited';
		public static const CORP_DELETED:String = 'corp_deleted';
		[Bindable]
		private var _corpList:ArrayCollection = new ArrayCollection();
		
		public function get corpList():ArrayCollection
		{
			return _corpList;
		}
		[Bindable]
		public function set corpList(value:ArrayCollection):void
		{
			_corpList =  value;
		}
		
		private var _editingCorp:CorpVO;
		
		public function get editingCorp():CorpVO
		{
			return _editingCorp;
		}
		
		public function set editingCorp(value:CorpVO):void
		{
			_editingCorp = value;
			editCorp(_editingCorp);
		}
		
		
		private function editCorp(vo:CorpVO):void
		{
			for (var i:int = 0;i<corpList.length;i++)
			{
				if(corpList[i].corpName == vo.corpName)
				{
					/*corpList.removeItemAt(i);
					corpList.refresh();
					corpList.addItemAt(vo,i);
					corpList.refresh();*/
					
					corpList[i] = vo;
					
				}
			}
			
			dispatch(new Event(CorpModel.CORP_EDITED));
		}
		
		private var _deleteCorp:CorpVO;
		
		public function get deleteCorp():CorpVO
		{
			return _deleteCorp;
		}
		
		public function set deleteCorp(value:CorpVO):void
		{
			_deleteCorp = value;
			onDelete(_deleteCorp)
		}
		
		private function onDelete(vo:CorpVO):void
		{
			for (var i:int = 0;i<corpList.length;i++)
			{
				if(corpList[i].corpName == vo.corpName)
				{
					corpList.removeItemAt(i);
				}
			}
			
			dispatch(new Event(CorpModel.CORP_DELETED));
		}
	}
}